﻿using System;
using System.Collections.Generic;
using ZilLion.Core.Unities.UnitiesMethods;
using ZilLion.Service.Core.Log;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.CrmProxy;
using ZilLion.Service.WeChatMall.Domain.Requests.CrmProxy;
using ZilLion.Service.WeChatMall.Domain.Responses.CrmProxy;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys
{
    public class CrmProxyRepository : ICrmProxyRepository
    {
        public AccWalletSingleResponse GetAccInfo(string passport, string getwey, PassportType passporttype)
        {
            var url = $"{getwey}/ApiV1/CRMYwDzService/AccWallet_Single_Query";
            var result = HttpHelper.HttpRequestGet<AccWalletSingleResponse>(url,
                new Dictionary<string, string>
                {
                    {"passport", passport},
                    {"passporttype", ((int) passporttype).ToString()}
                },
                content => content.ReadAsStringAsync().Result);
            return result;
        }

        public CrmTransactionFlowingResponse GetTransactionFlowing(string passport, string getwey, PassportType passporttype)
        {
            var url = $"{getwey}/ApiV1/CRMYwDzService/AccOprInfo_Single_QueryBalanceChange";
            var result = HttpHelper.HttpRequestGet<CrmTransactionFlowingResponse>(url,
                new Dictionary<string, string>
                {
                    {"passport", passport},
                    {"passporttype", ((int) passporttype).ToString()}
                },
                content => content.ReadAsStringAsync().Result);
            return result;
        }

        public AccCashPayResponse CashPay(CrmCashPayRequest request, string passport, string getway,
            PassportType passporttype)
        {

           

            AccCashPayResponse result = null;
           
            try
            {
                var url = $"{getway}/ApiV1/CRMYwDzService/AccXf_Single_Xf";
                 result = HttpHelper.HttpRequestPost<CrmCashPayRequest, AccCashPayResponse>(url, request,
                    new Dictionary<string, string>
                    {
                        {"passport", passport},
                        {"passporttype", ((int) passporttype).ToString()}
                    },60
                );
            }
            catch (Exception e)
            {
                ZilLionLogManager.Instance().Error(e);
                return new AccCashPayResponse(){Errormodel = new CrmErrormodel(){desc = "抱歉,支付系统出现故障"}};
            }
            return result;
        }
    }
}